package com.ruoyi.web.redisListener;

import com.ruoyi.system.domain.SpokenOrder;
import com.ruoyi.system.service.ISpokenOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;


@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    ISpokenOrderService orderService;

    @Autowired
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer){
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expireKey = message.toString();
        System.out.println("-------------redids key失效：" + expireKey);
        // 订单失效
        if(expireKey.startsWith("order-")){
            String orderNo = expireKey.split("-")[1];
            // 取消订单
            SpokenOrder spokenOrder = orderService.selectByNo(orderNo);
            if(spokenOrder != null){
                orderService.cancelOrder(spokenOrder.getOrderId());
            }
        }

    }
}
